<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="https://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>图片上传 &middot; 我的博客</title>
  <meta name="description" content="" />

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-09 ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       这里是任永刚的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>图片上传</h1>
  <time datetime=2020-04-29T00:00:00Z class="post-date">Wed, Apr 29, 2020</time>
  <h2 id="vue里的代码">vue里的代码</h2>
<pre><code>&lt;template&gt;
    &lt;div&gt;
        添加商品&lt;input v-model=&quot;name&quot;&gt;&lt;br&gt;
        价格&lt;input v-model=&quot;price&quot;&gt;&lt;br&gt;
        商品照片&lt;input type=&quot;file&quot; id=&quot;ssss&quot;&gt;&lt;br&gt;
        &lt;button @click=&quot;add&quot;&gt;添加&lt;/button&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
    export default {
        name: &quot;addcate&quot;,
        data:function () {
            return{
                name:'',
                price:'',
            }
        },
        methods: {
            add:function () {
                var data = new FormData();
                data.append('name',this.name);
                data.append('price',this.price);
                var image =document.getElementById('ssss').files[0];
                data.append('file',image);
                this.axios({
                    url:'/api/sadmin/addcate/',
                    data:data,
                    method:'post'
                }).then((res)=&gt;{
                    if (res.data.code==200){
                        this.$router.push({'path':'show'})
                    }
                    alert(res.data.message)
                }).catch((err)=&gt;{
                    console.log(err)
                })
            }
        }
    }
&lt;/script&gt;

&lt;style scoped&gt;

&lt;/style&gt;
</code></pre>
<h2 id="views里的代码">views里的代码</h2>
<pre><code>class Addcate(APIView):
    def post(self,request):
        res={}
        name = request.data.get('name')
        price = request.data.get('price')
        image = request.data.get('file')
        if not all([name,price,image]):
            res['code']=10020
            res['message']='输入不能为空'
        else:
            image_name = image.name
            image_path = os.path.join(settings.UPLOAD_FILE,image_name)
            f = open(image_path,'wb')
            for i in image.chunks():
                f.write(i)
            f.close()
            goods = Goods.objects.filter(name=name).first()
            if goods:
                res['code']=10023
                res['message']='商品已存在'
            else:
                goods = Goods(name=name,price=price,image='/upload/'+image_name)
                goods.save()
                res['code']=200
                res['message']='添加成功'
                return JsonResponse(res)
        return JsonResponse(res)
</code></pre>
<h2 id="settingspy-里面配置">settings.py 里面配置</h2>
<pre><code>STATICFLIES_DIRS=[os.path.join(BASE_DIR,'static')]#照片存放在static文件下
UPLOAD_FILE=os.path.join(BASE_DIR,'upload')#照片存放在根目录upload文件下</code></pre>

</div>


    </main>

    
      
    
  </body>
</html>
